package com.itheima.jobs;

import com.itheima.constant.RedisConstant;
import com.itheima.utils.QcloudUtils;
import org.springframework.beans.factory.annotation.Autowired;
import redis.clients.jedis.JedisPool;

import java.util.Date;
import java.util.Set;

public class ClearImgJob {
    @Autowired
    private JedisPool jedisPool;
    // 定期清理图片
    public void clearImg(){
//        每10s清理一次数据
        Set<String> sdiff = jedisPool.getResource().sdiff(RedisConstant.SETMEAL_PIC_RESOURCES, RedisConstant.SETMEAL_PIC_DB_RESOURCES);
        System.out.println(sdiff);
        for (String s : sdiff) {
            try {
                QcloudUtils.deleteObject(s);
                // 删除完成后,将多余得value删掉
                jedisPool.getResource().srem(RedisConstant.SETMEAL_PIC_RESOURCES,s);
            }catch (Exception e){

                System.out.println("删除图片出错=>"+e.getMessage());
            }

        }
        System.out.println("删除图片完成"+new Date());
    }
}
